package com.codingfuture.erp_system.service.impl;

import com.codingfuture.erp_system.dto.ReturnOrderDetailDto;
import com.codingfuture.erp_system.dto.ReturnOrdersDto;
import com.codingfuture.erp_system.entity.ReturnOrderDetailList;
import com.codingfuture.erp_system.entity.ReturnOrders;
import com.codingfuture.erp_system.entity.ReturnOrdersDetail;
import com.codingfuture.erp_system.entity.Store;
import com.codingfuture.erp_system.mapper.ReturnOrdersMapper;
import com.codingfuture.erp_system.service.ReturnOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class ReturnOrdersServiceImpl implements ReturnOrdersService {
    @Autowired
    private ReturnOrdersMapper returnOrdersMapper;


    @Override
    public List<ReturnOrders> findByAll(ReturnOrders returnOrders) {
        return returnOrdersMapper.findByAll(returnOrders);
    }


    @Override
    public List<ReturnOrders> salesReturnlistByPage() {
        return returnOrdersMapper.salesReturnlistByPage();
    }

    @Override
    public List<ReturnOrderDetailList> findReturnOrdersDto(long uuid) {
        return returnOrdersMapper.findReturnOrdersDetailDto(uuid);
    }

    @Override
    public List<Store> selectStore() {
        return returnOrdersMapper.selectStore();
    }

//    @Override
//    public Boolean returnDoCheck(Integer id) {
//        return returnOrdersMapper.returnDoCheck(id);
//    }

    @Override
    public List<ReturnOrderDetailList> findReturnOrderDetail(long uuid) {
        return returnOrdersMapper.findReturnOrderDetail(uuid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean returnDoCheck(Integer id, Date date) {
        return returnOrdersMapper.returnDoCheck(id, date);
    }

    @Override
    public List<ReturnOrders> salesReturnlistByPage(ReturnOrders returnOrders) {
        return returnOrdersMapper.salesReturnlistByPage(returnOrders);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean doInStore(Integer id, Date date) {
        return returnOrdersMapper.doInStore(id, date);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean doInStore(Integer id, Date date, Integer storeuuid) {
        int num = returnOrdersMapper.doInStore(id, date, storeuuid);
        int i = 1;
        List<ReturnOrderDetailDto> list = returnOrdersMapper.findById(id);
        for (ReturnOrderDetailDto returnOrderDetailDto : list) {
            Long returnordersuuid = returnOrderDetailDto.getGoodsuuid();
            List<ReturnOrderDetailDto> returnOrderDetailDtoList = returnOrdersMapper.findByUUid(returnordersuuid);
            for (ReturnOrderDetailDto detailDto : returnOrderDetailDtoList) {
                String state = detailDto.getState();
                if (Objects.equals(state, "0")) {
                    return i != 0;
                } else {
                    i = returnOrdersMapper.updateByState(returnordersuuid);
                }
            }

        }
        return num != 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addByOrdersDto(ReturnOrdersDto ordersDto) {
        return returnOrdersMapper.addByOrdersDto(ordersDto);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addByReturnOrderDetails(List<ReturnOrdersDetail> returnOrderDetails) {
        return returnOrdersMapper.addByReturnOrderDetails(returnOrderDetails);
    }

//    @Override
//    public Boolean returnDoCheck(Integer id, Date date) {
//
//        return returnOrdersMapper.returnDoCheck(id,date);
//    }

//    @Override
//    public List<Store> selectStore() {
//        return returnOrdersMapper.selectStore();
//    }
//
//    @Override
//    public List<ReturnOrders> findByAll(String state) {
//        return returnOrdersMapper.findByAll(state);
//    }
}
